#include <iostream>

using namespace std;
const int N = 1000;
 int n;
int s[N]; // 酸
int b[N]; // 苦
bool st[N];//储存标记，分辨每种组合是否用过
int min_sub = 98778; // 初始化很大的值方便第一次更新
void dfs(int x, int mul, int sum)
{ 
    if (x > n) //说明已经枚举完所有的组合
    {
        bool has_selected = false;
        for (int i = 1; i <= n; i++)
        { 
            if(st[i])
            {
                has_selected = true;
                break;
            }
        }
        if (has_selected)//如果有组合,则尝试更新一下答案
        {
            int diff = abs(mul - sum);
            if (diff < min_sub)
            {
                min_sub = diff;
            }
        }
        return;
    }
    //选
    st[x] = true;
    dfs(x + 1, mul * s[x], sum + b[x]);
    // 不选
    st[x] = false;
    dfs(x + 1, mul, sum);
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> s[i] >> b[i];
    }
    dfs(1, 1, 0);
    cout << min_sub << endl;
    return 0;
}



























#include <iostream>

using namespace std;
const int N = 20;
int acid[N];
int bitter[N];
int n;
int res;//记录符合的种类数
bool st[N];
int min_sub = 54465;//初始化一个很大的数字，方便第一次更新
void dfs(int x, int mul, int sum)
{
    if (x > n)
    {
        bool has_selected = false;
        //题目要求至少选一种,要检查是不是至少选了一种调料，不能出现一个调料都不选的组合
        for (int i = 1; i <= n; i++)
        {
            if (st[i])
            { 
                has_selected = true;
                break;
            }
        }
        //如果存在合法的组合，那么就要检查它的酸度和苦度绝对值之差，尝试更新答案
        if (has_selected)
        {
            int diff = abs(mul - sum);
            if (diff < min_sub)
                min_sub = diff;
        }

            return;
    }
    //选
    st[x] = true;
    dfs(x + 1, acid[x]*mul, bitter[x]+sum);
    // 不选
    st[x] = false;
    dfs(x + 1, mul, sum);
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    //输入每种调料的酸度和苦度
    for (int i = 1; i <= n; i++)
    { 
        cin >>acid[i] >> bitter[i];
    }
    dfs(1, 1,  0);
    cout << min_sub << endl;
    return 0;
}
